package com.ctvit.sms.platform.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.dao.OrderPart;
import com.ctvit.framework.service.BaseService;
import com.ctvit.sms.platform.domain.MsgReg;
import com.ctvit.sms.platform.domain.MsgReq;
import com.ctvit.sms.platform.domain.ReqCommonmsg;
import com.ctvit.sms.platform.domain.ReqMamdownload;
import com.ctvit.sms.platform.domain.ReqMeetingnotice;
import com.ctvit.sms.platform.domain.ReqProbroadcast;
import com.ctvit.sms.platform.domain.ReqSystemerror;
import com.ctvit.sms.platform.domain.ReqWorkflow;
import com.ctvit.sms.platform.domain.SystemConfig;
import com.ctvit.struts.common.CommonFunc;

public class ReqTaskService extends BaseService {
	private SmsService smsService;

	public SmsService getSmsService() {
		return smsService;
	}

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	/**
	 * 检查是否有新的满足条件的请求生成短信任务
	 */
	public void checkNewReq() {
		log.info("begin check qualified msg request");
		Condition condition = new Condition();
		String delayString = "00:15:00";// 默认15分钟
		if (CommonFunc.delayString.equals("")) {
			condition.add("zCfgId", "REQDELAY");// 时间格式的4位数
			SystemConfig sConfig = (SystemConfig) genericDao.getObjectByCondition(SystemConfig.class, condition);
			if (sConfig != null) {
				delayString = sConfig.getZCfgVal();
				// delayString=delayString.substring(0,2)+":"+delayString.substring(2,4)+":"+delayString.substring(4,6);
			}
			CommonFunc.delayString = delayString;
		} else {
			delayString = CommonFunc.delayString;
		}
		condition = new Condition();
		OrderPart orders = new OrderPart();
		orders.add("zCrtTime", OrderPart.ASC);
		condition.add("", "z_msg_status='2' and z_msg_type='1' and now()>=z_crt_time+'" + delayString + "'", "", true);
		List lst = genericDao.getByCondition(MsgReq.class, condition, orders);
		if (lst != null && lst.size() > 0) {
			MsgReq mReq = null;
			MsgReg mReg = null;
			for (int i = 0; i < lst.size(); i++) {
				mReq = (MsgReq) lst.get(i);
				condition = new Condition();
				condition.add("zRegNo", mReq.getZRegId());
				mReg = (MsgReg) genericDao.getObjectByCondition(MsgReg.class, condition);
				smsService.doGenerateTask(mReq, mReg);
			}
			if (lst != null && lst.size() > 0) {
				log.info("本次共处理" + lst.size() + "个请求");
			}
		} else {
			// log.info("未查到需要处理的请求");
		}
	}

	public static Calendar getStartTime() {
		Calendar cal = Calendar.getInstance();
		// cal.set(Calendar.AM_PM, Calendar.PM);
		// cal.set(Calendar.HOUR, 12);
		// cal.set(Calendar.MINUTE, 00);
		// cal.set(Calendar.SECOND, 00);
		return cal;
	}

	public void startTimer() {
		Condition condition = new Condition();
		int intervalSec = 120;// 默认检查间隔120秒
		if (CommonFunc.intervalSec == 0) {
			condition.add("zCfgId", "CHECKINTERVAL");// 秒数
			SystemConfig sConfig = (SystemConfig) genericDao.getObjectByCondition(SystemConfig.class, condition);
			if (sConfig != null) {
				intervalSec = Integer.parseInt(sConfig.getZCfgVal());
			}
			CommonFunc.intervalSec = intervalSec;
		} else {
			intervalSec = CommonFunc.intervalSec;
		}
		final Timer impTimer = new Timer();
		Calendar cal = getStartTime();
		final Date fstTime = cal.getTime();
		impTimer.schedule(new java.util.TimerTask() {
			public void run() {
				try {
					checkNewReq();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, fstTime, intervalSec * 1000);
	}

	/**
	 * 
	 * @ps:
	 * @param msgReq
	 * @return
	 */
	private boolean saveMsgReq(MsgReq msgReq) {
		// 查询注册编号和系统编号在注册表里是否存在
		StringBuffer stringBuffer = new StringBuffer("select z_reg_no from t_msg_reg where z_reg_no ='");
		stringBuffer.append(msgReq.getZRegId()).append("' ");
		List<Map<String, ?>> list = genericDao.executeSql(stringBuffer.toString());
		int result = list.size(); // 查询后的结果集大小
		Condition condition = new Condition();
		condition.add("zReqId", msgReq.getZReqId());
		int i = genericDao.updateByCondition(msgReq, null, condition);// 查询在请求表中是否存在
		if (result > 0 && i == 0) {
			msgReq.setZMsgStatus("2");// 存在则将信息状态设置为2
			genericDao.insert(msgReq);
			return true;
		} else {
			msgReq.setZMsgStatus("0");// 不存在则设为0
			genericDao.insert(msgReq);
		}
		return false;
	}

	public int saveMeetingNotice(MsgReq msgReq, ReqMeetingnotice reqMeetingnotice) {
		if (reqMeetingnotice == null)
			return -1;
		saveMsgReq(msgReq); // 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqMeetingnotice.getId());
		int i = genericDao.updateByCondition(reqMeetingnotice, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqMeetingnotice);
			return 0;
		}
		return 1;
	}

	public int saveProBroadcast(MsgReq msgReq, ReqProbroadcast reqProbroadcast) {
		if (reqProbroadcast == null)
			return -1;
		saveMsgReq(msgReq);// 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqProbroadcast.getId());
		int i = genericDao.updateByCondition(reqProbroadcast, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqProbroadcast);
			return 0;
		}
		return 1;
	}

	public int saveMAMDownload(MsgReq msgReq, ReqMamdownload reqMamdownload) {
		if (reqMamdownload == null)
			return -1;
		saveMsgReq(msgReq);// 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqMamdownload.getId());
		int i = genericDao.updateByCondition(reqMamdownload, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqMamdownload);
			return 0;
		}
		return 1;
	}

	public int saveWorkFlow(MsgReq msgReq, ReqWorkflow reqWorkflow) {
		if (reqWorkflow == null)
			return -1;
		saveMsgReq(msgReq);// 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqWorkflow.getId());
		int i = genericDao.updateByCondition(reqWorkflow, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqWorkflow);
			return 0;
		}
		return 1;
	}

	public int saveSystemError(MsgReq msgReq, ReqSystemerror reqSystemerror) {
		if (reqSystemerror == null)
			return -1;
		saveMsgReq(msgReq);// 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqSystemerror.getId());
		int i = genericDao.updateByCondition(reqSystemerror, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqSystemerror);
			return 0;
		}
		return 1;
	}

	public int saveCommonMsg(MsgReq msgReq, ReqCommonmsg reqCommonmsg) {
		if (reqCommonmsg == null)
			return -1;
		saveMsgReq(msgReq);// 保存req表的内容
		Condition condition = new Condition();
		condition.add("id", reqCommonmsg.getId());
		int i = genericDao.updateByCondition(reqCommonmsg, new String[] { "id" }, condition);
		if (i == 0) {
			genericDao.insert(reqCommonmsg);
			return 0;
		}
		return 1;
	}
}
